package com.gxuwz.check.dao.teacher.impl;

import com.gxuwz.check.base.BaseDao;
import com.gxuwz.check.dao.teacher.ITeacherDao;
import com.gxuwz.check.model.dto.ClassDto;
import com.gxuwz.check.model.dto.CourseDto;
import com.gxuwz.check.model.entity.SysCheckEntity;
import com.gxuwz.check.model.entity.SysCourseEntity;
import com.gxuwz.check.model.entity.SysStudentEntity;
import com.gxuwz.check.util.DateUtil;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Repository("TeacherDaoImpl")
public class TeacherDaoImpl extends BaseDao implements ITeacherDao {

    @Override
    @Transactional
    public List<ClassDto> classList(int userId) {
        //首先查出我所带的班级
        String sql = "select class_id from sys_teacher where user_id = " + userId;
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);
        sqlQuery.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        List list = sqlQuery.list();
        System.out.println("course_id  " + list);
        String[] classId = list.get(0).toString().replace("{class_id=", "").replace("}", "").split(",");
        //查询班级
        List<ClassDto> listDto = new ArrayList<>();
        for (int i = 0; i < classId.length; i++) {
            try {
                ClassDto classDto = new ClassDto();
                String hql = "select s.className from SysClassEntity s where classId = ?";
                List list1 = getHibernateTemplate().find(hql, Integer.valueOf(classId[i]));
                classDto.setClassId(Integer.parseInt(classId[i]));
                classDto.setClassName((String) list1.get(0));
                listDto.add(classDto);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        SessionClose();
        return listDto;
    }

    /**
     * 根据教师的ID查询出他所教的课程
     *
     * @param userId
     * @return
     */
    @Override
    public List<CourseDto> findCourseByTeacherId(int userId) {
        String hql1 = "from SysCourseEntity c where c.teacherId = " + userId;
        List<SysCourseEntity> listCourse = (List<SysCourseEntity>) getHibernateTemplate().find(hql1);
        List<CourseDto> listDto = new ArrayList<>();
        for (int i = 0; i < listCourse.size(); i++) {
            try {
                CourseDto courseDto = new CourseDto();
                courseDto.setCourseId(listCourse.get(i).getCourseId());
                courseDto.setCourseName(listCourse.get(i).getCourseName());
                listDto.add(courseDto);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        return listDto;
    }

    /**
     * 缺勤添加
     * @param studentIdArray
     * @param classIdSelect
     * @param courseId
     * @param userIdNow
     */
    @Override
    @Transactional
    public void submitChecks(String[] studentIdArray, int classIdSelect, int courseId, int userIdNow) {
        String date = DateUtil.dataToString(new Date());
        for (int i = 0; i < studentIdArray.length; i++) {
            String hql = "from SysCheckEntity where studentId = ?";
            List list = getHibernateTemplate().find(hql, Integer.valueOf(studentIdArray[i]));
            if (list.size() == 0) {
                String sql = "insert into sys_check(check_time,student_id,teacher_id,absence_num,course_id,class_id) value(" + date + "," + studentIdArray[i] + "," + userIdNow + "," + 1 + "," + courseId + "," + classIdSelect + ") ";
                getSession().createSQLQuery(sql).executeUpdate();
            } else {
                try {
                    String hqls = "UPDATE SysCheckEntity set absenceNum = absenceNum+1 where studentId = :studentId";
                    Query query = getSession().createQuery(hqls);
                    query.setParameter("studentId", Integer.valueOf(studentIdArray[i]));
                    int res = query.executeUpdate();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        SessionClose();
    }

    @Override
    @Transactional
    public Map<String,List> getAbsenceMaxTen(int classIdCharts) {
        Criteria criteria = getSession().createCriteria(SysCheckEntity.class);
        criteria.add(Restrictions.eq("classId",Integer.valueOf(classIdCharts)));
        List<SysCheckEntity> list = criteria.list(); //根据班级ID查出考勤表里面的学生
        List<String> listStuName = new ArrayList<>();
        List<Integer> listAbsenceNum = new ArrayList();
        for (int i = 0 ; i < list.size() ; i++){
            try {
                Criteria criteria1 = getSession().createCriteria(SysStudentEntity.class);
                criteria1.add(Restrictions.eq("userId",Integer.valueOf(list.get(i).getStudentId())));
                List<SysStudentEntity> list1 = criteria1.list();
                listStuName.add(list1.get(0).getStudentName());
                listAbsenceNum.add(list.get(i).getAbsenceNum());
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        Map<String,List> map = new HashMap<>();
        map.put("listStuName",listStuName);
        map.put("listAbsenceNum",listAbsenceNum);
        SessionClose();
        return map;
    }
}
